//
//  UserBirthdayVC.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/27.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserBirthdayVC.h"
#import "FlatUIKit.h"
#import "NSDate+FormateString.h"

@interface UserBirthdayVC ()<UITextFieldDelegate>

/** textField */
@property (nonatomic, strong) FUITextField *textField;

@end

@implementation UserBirthdayVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.view.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    
    [self setBirthdayUpUI];
    
    [self settingKeyBoard];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self.textField becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    if (self.selectBirthdayBlock) {
        self.selectBirthdayBlock(self.textField.text);
    }
}

- (void)setBirthdayUpUI {
    FUITextField *textField = [[FUITextField alloc] initWithFrame:CGRectMake(0, kAutoHei(24), kYFScreenWidth, kAutoHei(44))];
    
    self.textField = textField;
    self.textField.tintColor = [UIColor clearColor];
    textField.textColor = [UIColor colorWithHex:0x787878];
    textField.font = [UIFont systemFontOfSize:16];
    
    
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textField];
    textField.delegate = self;
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.text = NSLocalizedString(@"Birthday", nil);
    
    leftLabel.textColor = [UIColor colorWithHex:0x151515];
    leftLabel.font = [UIFont systemFontOfSize:18];
    
    [leftLabel sizeToFit];
    CGFloat labelW = leftLabel.bounds.size.width;
    
    leftLabel.frame = CGRectMake(0, 0,(labelW + kAutoWid(40)) , kAutoHei(44));
    leftLabel.textAlignment = NSTextAlignmentCenter;
    
    self.textField.leftView = leftLabel;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    textField.edgeInsets = UIEdgeInsetsMake(0, kAutoWid(10), 0, 0);
    
    self.textField.text = self.currentBirthdayDate;
}
// 给生日输入框自定义键盘
- (void)settingKeyBoard
{
    UIDatePicker *dateView = [[UIDatePicker alloc] init];
    dateView.datePickerMode = UIDatePickerModeDate;
    
    if ([NSBundle currentLanguageIsChinese]) {
        dateView.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];
    }else {
        dateView.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
    }
    
    dateView.minimumDate = [NSDate formateString:@"1940-01-01 00:00:00"];
    dateView.maximumDate = [NSDate date];
    if (self.currentBirthdayDate.length > 0) {
        dateView.date = [NSDate formateYearMonthDayString:self.currentBirthdayDate];
    }

    dateView.backgroundColor = [UIColor whiteColor];
    
    self.textField.inputView = dateView; // 给生日输入框添加键盘
    // 监听生日键盘的改变
    [dateView addTarget:self action:@selector(brithdayChange:) forControlEvents:UIControlEventValueChanged];
}

#pragma mark - 生日改变
- (void)brithdayChange:(UIDatePicker *)dateView
{
    // 取到当前选择的时间
    NSString *time = [dateView.date formateYearMonthDay];;
    
    self.textField.text = time;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return NO;
}


@end
